package com.yingxin.yxerp.module.sys.controller;

import com.yingxin.yxerp.module.sys.entity.BusinessTypeSetting;
import com.yingxin.yxerp.module.sys.mapper.BusinessTypeSettingMapper;
import com.yingxin.yxerp.module.sys.service.IBusinessTypeSettingService;
import com.yingxin.yxerp.module.sys.vo.req.BusinessTypeSettingQueryVo;
import com.yingxin.yxerp.module.sys.vo.req.BusinessTypeSettingUpdateVo;
import com.yingxin.yxerp.module.sys.vo.resp.BusinessTypeSettingRespVo;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 每日编号 前端控制器
 * @author fanchao
 * @since 2022-01-16
 */

@RestController
@RequestMapping("/sys/daily-no-setting")
public class BusinessTypeSettingController {

    @Autowired
    private IBusinessTypeSettingService businessTypeSettingService;

    @SuppressWarnings("all")
    @Autowired
    private BusinessTypeSettingMapper businessTypeSettingMapper;


    @ApiOperation(value = "查询列表（全部）")
    @PostMapping("/list")
    public List<BusinessTypeSettingRespVo> list(@RequestBody BusinessTypeSettingQueryVo queryVo) {
        return businessTypeSettingMapper.listVo(queryVo);
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping("/{id}")
    public BusinessTypeSettingRespVo get(@PathVariable("id") String id) {
        BusinessTypeSettingRespVo entity = businessTypeSettingMapper.getVo(id);
        return entity;
    }

    @PreAuthorize("hasAuthority('dailyNoSetting:update')")
    @ApiOperation(value = "修改")
    @PutMapping
    public String update(@RequestBody @Valid BusinessTypeSettingUpdateVo updateVo) {
        BusinessTypeSetting entity = new BusinessTypeSetting();
        entity.setId(updateVo.getId());
        if (StringUtils.isNotBlank(updateVo.getName())) {
            entity.setName(updateVo.getName());
        }
        entity.setPrefix(updateVo.getPrefix());
        entity.setDatePattern(updateVo.getDatePattern());
        entity.setDailyNoLength(updateVo.getDailyNoLength());
        businessTypeSettingService.update(entity);
        return entity.getId();
    }
}
